<?php
namespace Home\Controller;
class IndexController extends CommonController {

    public function index(){
        /**
         * 前台首页
         */
        $this->display();
    }
    //自定义函数,page分页信息
      public function N($val){
         $a = M('configs')->field('value')->select();
        return $a[$val]['value'];
    }
    //新闻
    public function Article(){
          //广告片
        $this->add=M('ad')->field('adthumb,name')->select();
        $Page = new \Think\Page(M('page')->where(array('status'=>1))->count(),$this->N(1));// 实例化分页类
        $this->Ar=M('page')
                        ->order('aid desc')
                        ->where(array('status'=>1))
                        ->field('aid,title,summer,addTime,thumb,click')
                        ->limit($Page->firstRow.','.$Page->listRows)
                        ->select();
        $Page->setConfig('next','كىيىنكى بەت');
        $Page->setConfig('prev','ئالدىنقى بەت');
        $this->Page=$Page->show();
        $this->display();
    }

    //阅读新闻
    public function view($aid){
        $views=M('page')->where(array('aid'=>$aid))->find();
        $views['content']=html_entity_decode($views['content']);
        //文章自增点击量，thinkphp提供的功能
        M('page')->where(array('aid'=>$aid))->setInc('click',1);
        $this->views=$views;
        //要获取这篇文章里面的所有tag
        $this->tags=M('page_tag')
                    ->join('page ON page.aid=page_tag.aid')
                    ->join('tag ON tag.tid=page_tag.tid')
                    ->where(array('page.aid'=>$aid))
                    ->select();
        //相关文章
        $category_cid=M('page')->where(array('aid'=>$aid))->getField('category_cid');
        $this->article=M('page')->where(array('category_cid'=>$category_cid))
                        ->limit(4)
                        ->order('click desc')->field('thumb,title,aid')
                        ->select();
        $this->display();
    }


    //教程
    public function video(){
        $this->display();
    }
    //分类
    public function category($cid)
    {

        if(!$cid) $this->redirect('Index/index');
        $Page = new \Think\Page(M('page')->where(array('status'=>1,'category_cid'=>$cid))->count(),$this->N(1));// 实例化分页类
        $this->Ar=M('page')
            ->order('aid desc')
            ->where(array('status'=>1,'category_cid'=>$cid))
            ->field('aid,title,summer,addTime,thumb,click')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $Page->setConfig('next','كىيىنكى بەت');
        $Page->setConfig('prev','ئالدىنقى بەت');
        $this->Page=$Page->show();

        $this->display();
    }

    //tags
    public function tag($tid)
    {

        if(!$tid) $this->redirect('Index/index');
//        p(M('page_tag')->where(array('tid'=>$tid))->count());die;
        $Page = new \Think\Page(M('page_tag')->where(array('tid'=>$tid))->count(),$this->N(1));// 实例化分页类
        $this->Ar=M('page_tag')                      //要获取这个tag里面的文章
            ->join('page ON page_tag.aid=page.aid')
            ->order('page.aid desc')
            ->where(array('status'=>1,'tid'=>$tid))
            ->field('page.aid,title,summer,addTime,thumb,click,tid')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $Page->setConfig('next','كىيىنكى بەت');
        $Page->setConfig('prev','ئالدىنقى بەت');
        $this->Page=$Page->show();
        $this->display();
    }
}